package com.sinoglobal.dumpling.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import static com.sinoglobal.dumping.base.Dumpling_SinoValueManager.*;

import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.base.Dumpling_SinoValueManager;
import com.sinoglobal.wallet.app.SinoValueManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by KeZhuang on 2016/1/11.
 */
public class Dumpling_DataRecycleServer extends Service{
    private static final long TIME_1_M = 1000 * 60;
    private static final long TIME_30_S = TIME_1_M / 2;
    private static final long TIME_1_H = 1000 * 60 * 60;
    private static final long TIME_24_H =TIME_1_H * 24 + 1;
    private long dawnTime;
    private long currentTime;
    private Thread mThread = new Thread(){
        @Override
        public void run() {
            super.run();
            dawnTime = getDawnTime();
            putValue(getApplicationContext(),Dumpling_SinoConstans.DUMPLING_TIME,dawnTime,true);
            for(;;){
                currentTime = getCurrentTime();
                if(currentTime>=dawnTime){
                    dawnTime = getDawnTime();
                    putValue(getApplicationContext(),Dumpling_SinoConstans.DUMPLING_TIME,dawnTime,true);
                    clearAllDumpling(getApplicationContext());
                }
                try {
                    sleep(TIME_30_S);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (getCurrentState() == Dumpling_SinoConstans.LOGIN_STATE_QUIT ||
                getCurrentState()==Dumpling_SinoConstans.LOGIN_STATE_LOGOUT) {
            int dumplingCode = (Integer) getValue(getApplicationContext(),Dumpling_SinoConstans.DUMPLING_CODE,0);
            if(dumplingCode==0||dumplingCode==Dumpling_SinoConstans.SUCCESS_CODE){
                clearAllDumpling(getApplicationContext());
            }
        }
        mThread.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    /** 获取第二天0点的时间*/
    private long getDawnTime(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String time = year+"-"+month+"-"+day+" "+"00:00:00";
        long millsecond = 0;
        try {
            millsecond =format.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(millsecond!=0){
            millsecond+= TIME_24_H;
        }
        return millsecond;
    }
    private long getCurrentTime(){
        Calendar calendar = Calendar.getInstance();
        return calendar.getTime().getTime();
    }
    private int getCurrentState() {
        return (Integer) Dumpling_SinoValueManager.getValue(getApplicationContext(), Dumpling_SinoConstans.DUMPLING_MARK_LOGIN, Dumpling_SinoConstans.LOGIN_STATE_LOGOUT);
    }
}
